from datetime import datetime
import uuid
from pony import orm

from .config import settings

# orm.set_sql_debug(True)


db = orm.Database()
db.bind(
    provider='postgres',
    host=settings.PG_HOST_ALG_LOCAL,
    port=settings.PG_PORT_ALG_LOCAL,
    database=settings.PG_DATABASE_ALG_LOCAL,
    user=settings.PG_USERNAME_ALG_LOCAL,
    password=settings.PG_PASSWORD_ALG_LOCAL,
)


def generate_uuid():
    return uuid.uuid4().hex

################
# place all entities below
################

# Required, 必须有值的字段, 值不能是null或者''
# Optional, 值可以为null或者''的字段, 字符字段默认为'', 其它字段默认为null

class Hospital(db.Entity):
    _table_ = 'inphile_big_data_hospital'

    id = orm.PrimaryKey(str, default=generate_uuid)
    hospital_id = orm.Optional(str)
    hospital = orm.Optional(str)
    img_original_name = orm.Optional(str)
    full_name = orm.Optional(str)
    customer_name = orm.Optional(str)
    nhsa_name = orm.Optional(str)
    nhsa_code = orm.Optional(str)
    price = orm.Optional(str)
    medical_insurance_type = orm.Optional(str)
    ratio = orm.Optional(str)
    type = orm.Optional(str)
    remark = orm.Optional(str)
    img_path = orm.Optional(str)
    # 创建时间：设置为标准时间UTC，这样可以节省每次转换所需要的开销，提高性能。
    # creation_time = orm.Required(datetime, sql_type='TIMESTAMP WITH TIME ZONE', volatile=True, default=utc_now())
    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())
    # 最近修改时间
    # last_modified = orm.Optional(datetime, sql_type='TIMESTAMP WITH TIME ZONE', volatile=True)


class HospitalOld(db.Entity):
    _table_ = 'inphile_big_data_hospital_old'

    id = orm.PrimaryKey(str, default=generate_uuid)
    hospital_id = orm.Optional(str)
    hospital = orm.Optional(str)
    img_original_name = orm.Optional(str)
    full_name = orm.Optional(str)
    customer_name = orm.Optional(str)
    nhsa_name = orm.Optional(str)
    nhsa_code = orm.Optional(str)
    price = orm.Optional(str)
    medical_insurance_type = orm.Optional(str)
    ratio = orm.Optional(str)
    type = orm.Optional(str)
    remark = orm.Optional(str)
    img_path = orm.Optional(str)
    # 创建时间：设置为标准时间UTC，这样可以节省每次转换所需要的开销，提高性能。
    # creation_time = orm.Required(datetime, sql_type='TIMESTAMP WITH TIME ZONE', volatile=True, default=utc_now())
    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())
    # 最近修改时间
    # last_modified = orm.Optional(datetime, sql_type='TIMESTAMP WITH TIME ZONE', volatile=True)


class HospitalTemp(db.Entity):
    _table_ = 'inphile_big_data_hospital_copy2'

    id = orm.PrimaryKey(str, default=generate_uuid)
    hospital_id = orm.Optional(str)
    hospital = orm.Optional(str)
    img_original_name = orm.Optional(str)
    full_name = orm.Optional(str)
    customer_name = orm.Optional(str)
    nhsa_name = orm.Optional(str)
    nhsa_code = orm.Optional(str)
    price = orm.Optional(str)
    medical_insurance_type = orm.Optional(str)
    ratio = orm.Optional(str)
    type = orm.Optional(str)
    remark = orm.Optional(str)
    img_path = orm.Optional(str)
    # 创建时间：设置为标准时间UTC，这样可以节省每次转换所需要的开销，提高性能。
    # creation_time = orm.Required(datetime, sql_type='TIMESTAMP WITH TIME ZONE', volatile=True, default=utc_now())
    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())
    # 最近修改时间
    # last_modified = orm.Optional(datetime, sql_type='TIMESTAMP WITH TIME ZONE', volatile=True)


class City(db.Entity):
    _table_ = 'city'

    id = orm.PrimaryKey(str, default=generate_uuid)
    city = orm.Optional(str)
    original_name = orm.Optional(str)
    full_name = orm.Optional(str)
    customer_name = orm.Optional(str)
    medical_insurance_type = orm.Optional(str)
    ratio = orm.Optional(str)
    type = orm.Optional(str)
    remark = orm.Optional(str)
    # 创建时间
    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())
    # 最近修改时间
    # last_modified = orm.Optional(datetime, sql_type='TIMESTAMP WITH TIME ZONE', volatile=True)


class Province(db.Entity):
    _table_ = 'province'

    id = orm.PrimaryKey(str, default=generate_uuid)
    province = orm.Optional(str)
    original_name = orm.Optional(str)
    full_name = orm.Optional(str)
    customer_name = orm.Optional(str)
    type = orm.Optional(str)
    remark = orm.Optional(str)
    # 创建时间
    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())
    # 最近修改时间
    # last_modified = orm.Optional(datetime, sql_type='TIMESTAMP WITH TIME ZONE', volatile=True)


class Country(db.Entity):
    _table_ = 'inphile_big_data_country'

    id = orm.PrimaryKey(str, default=generate_uuid)
    full_name = orm.Optional(str)
    customer_name = orm.Optional(str)
    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())
    type = orm.Optional(str)


class Word_frequency(db.Entity):
    _table_ = 'word_frequency'

    id = orm.PrimaryKey(str, default=generate_uuid)
    hospital = orm.Optional(str)
    img_original_name = orm.Optional(str)
    num = orm.Optional(int, default=1, nullable=True)
    type = orm.Optional(str)

    # 创建时间
    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())
    # 最近修改时间
    last_modified = orm.Optional(datetime, volatile=True)


class Hospital_coding(db.Entity):
    _table_ = 'inphile_big_data_hospital_coding'

    id = orm.PrimaryKey(str, default=generate_uuid)
    hospital_id = orm.Optional(str)
    hospital_name_nhsa = orm.Optional(str)
    city = orm.Optional(str)
    province = orm.Optional(str)
    address = orm.Optional(str)
    level = orm.Optional(str)
    hospital_type = orm.Optional(str)
    remark = orm.Optional(str)
    # 创建时间
    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())
    # 最近修改时间
    last_modified = orm.Optional(datetime, volatile=True)


class Hospital_mapping(db.Entity):
    _table_ = 'inphile_big_data_hospital_mapping'

    id = orm.PrimaryKey(str, default=generate_uuid)
    hospital_id = orm.Optional(str)
    hospital_name_seal = orm.Optional(str)
    hospital_name_bill = orm.Optional(str)
    hospital_name_nhsa = orm.Optional(str)
    remark = orm.Optional(str)
    # 创建时间
    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())
    # 最近修改时间
    # last_modified = orm.Optional(datetime, sql_type='TIMESTAMP WITH TIME ZONE', volatile=True)


# 疾病映射累计报表
class Direct_mapping(db.Entity):
    _table_ = 'inphile_big_data_disease_direct_mapping'

    id = orm.PrimaryKey(str, default=generate_uuid)
    name = orm.Optional(str)
    customer_name = orm.Optional(str)
    assayer = orm.Optional(str)
    # 创建时间
    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())
    # 最近修改时间
    # last_modified = orm.Optional(datetime, sql_type='TIMESTAMP WITH TIME ZONE', volatile=True)


# 疾病
class Code_map(db.Entity):
    _table_ = 'inphile_big_data_disease_normal_mapping'

    id = orm.PrimaryKey(str, default=generate_uuid)
    normal_name = orm.Optional(str)
    normal_code = orm.Optional(str)
    customer_code = orm.Optional(str)
    customer_name = orm.Optional(str)
    remark = orm.Optional(str)
    # 创建时间
    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())
    # 最近修改时间
    # last_modified = orm.Optional(datetime, sql_type='TIMESTAMP WITH TIME ZONE', volatile=True)


# 疾病
class Party_a_data(db.Entity):
    _table_ = 'inphile_big_data_disease_customer_mapping'

    id = orm.PrimaryKey(str, default=generate_uuid)
    disease_code = orm.Optional(str)
    disease_name = orm.Optional(str)
    customer_name = orm.Optional(str)
    outsource = orm.Optional(str)
    remark = orm.Optional(str)
    # 创建时间
    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())
    # 最近修改时间
    # last_modified = orm.Optional(datetime, sql_type='TIMESTAMP WITH TIME ZONE', volatile=True)


# 疾病
class Department(db.Entity):
    _table_ = 'inphile_big_data_department_another_name'

    id = orm.PrimaryKey(str, default=generate_uuid)
    standard_name = orm.Optional(str)
    another_name = orm.Optional(str)
    remark = orm.Optional(str)
    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())


# 中草药
class Inphile_Alg_Chinese_Medicine(db.Entity):
    _table_ = 'inphile_alg_chinese_medicine'

    id = orm.PrimaryKey(str, default=generate_uuid)
    name = orm.Optional(str)
    create_time = orm.Required(datetime, volatile=True, default=datetime.now())


# 联想输入库
class ShuyuInput(db.Entity):
    _table_ = 'inphile_big_data_shuyu_input'

    id = orm.PrimaryKey(str, default=generate_uuid)
    hospital = orm.Optional(str)
    name = orm.Required(str)
    type = orm.Optional(str)
    num = orm.Required(int, default=0)
    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())
    last_modified = orm.Optional(datetime, volatile=True)


# 蓝卡业务联想输入法词库
class Inphile_Alg_Lancare_Input(db.Entity):
    _table_ = 'inphile_big_data_lancare_input'

    id = orm.PrimaryKey(str, default=generate_uuid)
    hospital = orm.Optional(str)
    name = orm.Required(str)
    type = orm.Optional(str)
    num = orm.Required(int, default=0)
    creation_time = orm.Required(datetime, volatile=True, default=datetime.now())
    last_modified = orm.Optional(datetime, volatile=True)


# json\pdf解析结果
class Bill(db.Entity):
    _table_ = 'inphile_alg_bill'

    id = orm.PrimaryKey(str, default=generate_uuid)
    app_name = orm.Optional(str)
    status = orm.Required(int, default=0)
    case_id = orm.Required(str)
    image_id = orm.Required(str, unique=True)
    image_path = orm.Required(str)
    bill_code = orm.Optional(str)
    bill_number = orm.Optional(str)
    bill_check_code = orm.Optional(str)
    bill_date = orm.Optional(str)
    bill_amount = orm.Optional(str)
    bill_payer = orm.Optional(str)
    result = orm.Optional(orm.Json)
    error = orm.Optional(str)
    remark = orm.Optional(str)
    operator = orm.Optional(str)
    operator_expire_time = orm.Optional(datetime, volatile=True)
    operator_submit_time = orm.Optional(datetime, volatile=True)
    create_time = orm.Required(datetime, volatile=True, default=datetime.now())
    end_time = orm.Optional(datetime, volatile=True)
    update_time = orm.Optional(datetime, volatile=True)
    biz_code = orm.Optional(str)
    callback_url = orm.Optional(str)

# 未带出明细电子票临时爬取库
class TmpCrawlRes(db.Entity):
    _table_ = 'tmp_crawl_res'

    id = orm.PrimaryKey(str, default=generate_uuid)
    im_path = orm.Optional(str)
    czb_path = orm.Optional(str)
    local_site_path = orm.Optional(str)
    crawl_status = orm.Optional(int)
    update_time = orm.Required(str)


# 医院别称库
class HospitalBiecheng(db.Entity):
    _table_ = 'inphile_big_data_hospital_biecheng'

    id = orm.PrimaryKey(str, default=generate_uuid)
    name = orm.Optional(str)
    another_name = orm.Optional(str)
    version = orm.Optional(str)
    update_time = orm.Required(str)


# nhsa
class NhsaChineseMedicine(db.Entity):
    _table_ = 'ods_nhsa_chinese_medicine_fordrink'

    id = orm.PrimaryKey(str, default=generate_uuid)
    piecesname = orm.Optional(str)

class NhsaConsumable(db.Entity):
    _table_ = 'ods_nhsa_consumable'

    id = orm.PrimaryKey(str, default=generate_uuid)
    productname = orm.Optional(str)

class NhsaMadeMedicine(db.Entity):
    _table_ = 'ods_nhsa_made_medicine'

    id = orm.PrimaryKey(str, default=generate_uuid)
    preparationprename = orm.Optional(str)

class NhsaMedicine(db.Entity):
    _table_ = 'ods_nhsa_medicine'

    id = orm.PrimaryKey(str, default=generate_uuid)
    registeredproductname = orm.Optional(str)
    goodscode = orm.Optional(str)

# nmpg
class NmpgDomesticMedicine(db.Entity):
    _table_ = 'ods_nmpg_domestic_medicine'

    id = orm.PrimaryKey(str, default=generate_uuid)
    name = orm.Optional(str)
    permission_number = orm.Optional(str)
    medicine_code = orm.Optional(str)


class NmpgImportMedicine(db.Entity):
    _table_ = 'ods_nmpg_import_medicine'

    id = orm.PrimaryKey(str, default=generate_uuid)
    name = orm.Optional(str)
    permission_number = orm.Optional(str)
    medicine_code = orm.Optional(str)

################
# place all entities above
################


# all entities connected to the database must be defined before calling generate_mapping()
# if the tables do not already exist, then they will be created
db.generate_mapping(create_tables=True)
# db.generate_mapping()
